﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Logging.LiveLogger;

namespace Calculator
{
	[Flags]
	public enum MetaKeyStatus
	{
		None = 0,
		Shift = 1,
		Control = 2,
		Alt = 4
	}

	public partial class MainForm : Form
	{
		NumPad mNumPad;

		public MainForm()
		{
			InitializeComponent();
			mNumPad = new NumPad(tlpNumPad);
			mNumPad.UpdateButtonText(mMetaKeyStatus);
		}

		#region Toggle Interface

		private MetaKeyStatus _mMetaKeyStatus = 0;
		private MetaKeyStatus mMetaKeyStatus
		{
			get
			{
				return _mMetaKeyStatus;
			}
			set
			{
				_mMetaKeyStatus = value;
			}
		}

		#endregion Toggle Interface

		#region Click Handlers

		private void btn7_Click(object sender, EventArgs e)
		{

		}

		private void btn8_Click(object sender, EventArgs e)
		{

		}

		private void btn9_Click(object sender, EventArgs e)
		{

		}

		private void btnAdd_Click(object sender, EventArgs e)
		{

		}

		private void btn4_Click(object sender, EventArgs e)
		{

		}

		private void btn5_Click(object sender, EventArgs e)
		{

		}

		private void btn6_Click(object sender, EventArgs e)
		{

		}

		private void btnSubtract_Click(object sender, EventArgs e)
		{

		}

		private void btn1_Click(object sender, EventArgs e)
		{

		}

		private void btn2_Click(object sender, EventArgs e)
		{

		}

		private void btn3_Click(object sender, EventArgs e)
		{

		}

		private void btnMultiply_Click(object sender, EventArgs e)
		{

		}

		private void btn0_Click(object sender, EventArgs e)
		{

		}

		private void btnDot_Click(object sender, EventArgs e)
		{

		}

		private void btnNeg_Click(object sender, EventArgs e)
		{

		}

		private void btnDivide_Click(object sender, EventArgs e)
		{

		}

		#endregion Click Handlers

		#region Key Listeners

		private void MainForm_KeyDown(object sender, KeyEventArgs e)
		{
			// Prevent Alt from shifting focus to non-existant menu, but still
			// allow Alt-F4 functionality.
			if (e.Alt && e.KeyCode != Keys.F4) { e.Handled = true; }

			// Update mMetaKeyStatus if necessary
			MetaKeyStatus newStatus = MetaKeyStatus.None;
			if (e.Alt)		{ newStatus = newStatus | MetaKeyStatus.Alt; }
			if (e.Control)	{ newStatus = newStatus | MetaKeyStatus.Control; }
			if (e.Shift)	{ newStatus = newStatus | MetaKeyStatus.Shift; }
			if (newStatus != mMetaKeyStatus) { mMetaKeyStatus = newStatus; }

			// Prevent keys from repeating
			if (mLastPressedKeys.HasFlag(e.KeyCode)) { return; }

			// Mark last pressed keys
			mLastPressedKeys = mLastPressedKeys | e.KeyCode;
			LiveLogger.Trace("KeyDown: " + mLastPressedKeys.ToString());

			// Forward the keycode to the appropriate button
			mNumPad.ForwardKeyDownToButton(e.KeyCode);

		}

		Keys mLastPressedKeys = Keys.None;

		private void MainForm_KeyUp(object sender, KeyEventArgs e)
		{
			// Update mMetaKeyStatus if necessary
			MetaKeyStatus newStatus = MetaKeyStatus.None;
			if (e.Alt)		{ newStatus = newStatus | MetaKeyStatus.Alt; }
			if (e.Control)	{ newStatus = newStatus | MetaKeyStatus.Control; }
			if (e.Shift)	{ newStatus = newStatus | MetaKeyStatus.Shift; }
			if (newStatus != mMetaKeyStatus) { mMetaKeyStatus = newStatus; }

			// Forward the keycode to the appropriate button
			mNumPad.ForwardKeyUpToButton(e.KeyCode);

			// Clear the last pressed keys
			mLastPressedKeys = mLastPressedKeys & ~e.KeyCode;
		}

		#endregion Key Listeners
	}
}
